package com.jk.smartelderlycare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.smartelderlycare.dao.SysRoleMapper;
import com.jk.smartelderlycare.dao.SysUserMapper;
import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.dto.SysUser;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.service.SysUserService;
import com.jk.smartelderlycare.utils.PaginationUtil;
import org.apache.catalina.realm.SecretKeyCredentialHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysUserServiceImpl
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/5 16:05
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;
    /** 
     * @description: 用户列表后端分页查询
     * @param: [sysUser, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysUser>
     * @author hhk
     * @date: 2024/11/5 16:06
     */ 
    @Override
    public PagedResponse<SysUser> findSysUserPaginationList(SysUser sysUser, Integer pageNum, Integer pageSize) {
        Page<SysUser> page = new Page<>(pageNum,pageSize);
        IPage<SysUser>  sysUserPageList =  sysUserMapper.findSysUserPaginationList(sysUser,page);
        return PaginationUtil.getPagedResponse(sysUserPageList.getRecords(),pageNum,pageSize,sysUserPageList.getTotal());
    }
    /** 
     * @description: 新增员工列表
     * @param: [sysUser]
     * @return: void
     * @author hhk
     * @date: 2024/11/5 16:06
     */ 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysUser(SysUser sysUser) {

        //更新系统时间
        sysUser.setCreateDate(new Date());
        //新增员工表
        sysUserMapper.insert(sysUser);
        //新增员工角色关联表sys_user_role
        HashMap<String, Object> roleMap = new HashMap<>();
        if(null!=sysUser.getRoleIds()){
            //循环roleIds
            for (Long roleId : sysUser.getRoleIds()) {
                roleMap.put("userId",sysUser.getUserId());
                roleMap.put("roleId",roleId);
                sysUserMapper.addSysUserAndRole(roleMap);
            }
        }
    }
    /** 
     * @description: 根据id删除一条信息
     * @param: [userId]
     * @return: void
     * @author hhk
     * @date: 2024/11/5 16:06
     */ 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysUserByUserId(Long userId) {
        //删除员工表
        sysUserMapper.deleteById(userId);
        //删除角色员工关联表
        sysUserMapper.deleteSysUserRoleByUserId(userId);

    }
    /** 
     * @description: 根据id回显一条数据
     * @param: [id]
     * @return: com.jk.smartelderlycare.dto.SysUser
     * @author hhk
     * @date: 2024/11/5 16:07
     */ 
    @Override
    public SysUser findSysUserByUserId(Long userId) {
        //根据id查询用户信息
        SysUser sysUser = sysUserMapper.findSysUserByUserId(userId);
        //特殊处理：角色编号给user实体中的roleIds
        //1.判断strRole值不为空
        if(null!= sysUser.getStrRole() && !"".equals(sysUser.getStrRole())){
            //2.获取strRole 并分割字符串，得到一个String[]数组
            String[] roleArr = sysUser.getStrRole().split(",");
            //3、创建list<Long>集合
            List<Long> roleList = new ArrayList<Long>();
            //4.循环数组：roleArr
            for (String roleId : roleArr) {
                //循环对象类型转换为Long并存入roleList集合
                roleList.add(Long.parseLong(roleId));
            }
            //5、将postList存入user对象中的roleIds
            sysUser.setRoleIds(roleList);
        }

        return sysUser;
    }
    /** 
     * @description: 修改员工列表
     * @param: [sysUser]
     * @return: void
     * @author hhk
     * @date: 2024/11/5 16:07
     */ 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysUser(SysUser sysUser) {
        //修改系统时间
        sysUser.setModifyDate(new Date());
        sysUser.setModifyUser("hhk");
        //一,修改员工表
        sysUserMapper.updateById(sysUser);
        //二、修改用户角色信息中间表：先删后增
        //1.根据用户id删除sys_user_role
        sysUserMapper.delUserRoleByUserId(sysUser.getUserId());
        //新增用户岗位关联关系表：sys_user_role
        //1.创建map集合
        HashMap<String, Object> roleMap = new HashMap<>();
        //循环roleIds
        for (Long roleId : sysUser.getRoleIds()) {
            roleMap.put("userId",sysUser.getUserId());
            roleMap.put("roleId",roleId);
            sysUserMapper.addSysUserAndRole(roleMap);
        }

    }
    /**
     * @description: 通过用户名获得用户信息
     * @param: [username]
     * @return: com.jk.smartelderlycare.dto.SysUser
     * @author hhk
     * @date: 2024/11/6 16:08
     */
    @Override
    public SysUser findSysUserByUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        return sysUserMapper.selectOne(queryWrapper);
    }
    /**
     * @description: 状态修改
     * @param: [sysUser]
     * @return: void
     * @author hhk
     * @date: 2024/11/6 20:29
     */
    @Override
    public void updateStatusById(SysUser sysUser) {
        sysUserMapper.updateStatusById(sysUser);
    }
    /**
     * @description: 角色动态下拉列表查询
     * @param: [sysRole]
     * @return: java.util.List<com.jk.smartelderlycare.dto.SysRole>
     * @author hhk
     * @date: 2024/11/6 20:29
     */
    @Override
    public List<SysRole> findSysRoleList() {
        return sysRoleMapper.selectList(null);
    }
    /**
     * @description: 根据当前登陆人id查询
     * @param: [username]
     * @return: com.jk.smartelderlycare.dto.SysUser
     * @author hhk
     * @date: 2024/11/11 10:05
     */
    @Override
    public List<SysUser> getSysUserByUsername(SysUser sysUser) {
        return sysUserMapper.getSysUserByUsername(sysUser);
    }
    /**
     * @description: 修改密码
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.dto.SysUser
     * @author hhk
     * @date: 2024/11/11 11:01
     */
    @Override
    public void upSysUserByPassWord(SysUser sysUser) {
         sysUserMapper.upSysUserByPassWord(sysUser);
    }

    /** 
     * @description: 用户表单表修改
     * @param: [sysUser]
     * @return: void
     * @author hhk
     * @date: 2024/11/12 14:26
     */ 
    @Override
    public void uptSysUser(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
    }

    /**
     * @description: 角色新增
     * @param: [sysRole]
     * @return: void
     * @author hhk
     * @date: 2024/11/12 20:37
     */
    @Override
    public void addSysRole(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }
}
